/**年度再培训的列表页面 */
<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.subject" placeholder="subject" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        Add
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        Export
      </el-button>
      <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        reviewer
      </el-checkbox>
    </div>
    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
      <el-table-column width="180px" align="center" label="培训编号" prop="id" />

      <el-table-column min-width="300px" label="培训主题">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.subject }}</span></template>
      </el-table-column>

      <el-table-column width="100px" label="培训开始时间">
        <template slot-scope="scope">
          <span>{{ scope.row.sdate }}</span></template>
      </el-table-column>

      <el-table-column width="100px" label="培训结束时间">
        <template slot-scope="scope">
          <span>{{ scope.row.edate }}</span></template>
      </el-table-column>

      <el-table-column align="center" label="培训人" width="95">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.trainer.userName }}</span></template>
      </el-table-column>

      <el-table-column align="center" label="受训人" width="95">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.trainee.userName }}</span></template>
      </el-table-column>
      
      <el-table-column class-name="status-col" label="所属Team" width="110">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)">{{ scope.row.deptName.deptName }}</span></template>
      </el-table-column>
    
      <el-table-column align="center" label="裁决状态" width="80" prop="" />

      <el-table-column align="center" label="操作" width="230">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fechData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="培训编号" prop="id">
          <el-input v-model="temp.id" />
        </el-form-item>
        <el-form-item label="培训主题" prop="subject">
          <el-input v-model="temp.subject" />
        </el-form-item>
        <el-form-item label="培训开始时间" prop="sdate">
          <el-input v-model="temp.sdate" />
        </el-form-item>
        <el-form-item label="培训结束时间" prop="edate">
          <el-input v-model="temp.edate" />
        </el-form-item>
        <el-form-item label="培训人" prop="trainer">
          <el-select v-if="temp.id>0" v-model="temp.trainer.userName" class="filter-item" placeholder="Please select">
            <el-option v-for="item in personlist" :key="item" :label="item" :value="item" />
        </el-select>
        </el-form-item>
        <el-form-item label="受训人" prop="trainee">
          <el-select v-if="temp.id>0" v-model="temp.trainee.userName" class="filter-item" placeholder="Please select">
            <el-option v-for="item in personlist" :key="item" :label="item" :value="item" />
        </el-select>
        </el-form-item>
        <el-form-item label="培训所属Team" prop="deptName">
          <el-select v-if="temp.id>0" v-model="temp.deptName.deptName" class="filter-item" placeholder="Please select">
            <el-option v-for="item in deptlist" :key="item" :label="item" :value="item" />
          </el-select>
          <!-- <el-input v-if="temp.faultId>0" v-model="temp.faultTeam.deptName" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchTrainReList, deleteTrainReList, editTrainRe, fetchAllCount, getPersonList, getDeptList } from '@/api/train-re'
import Sortable from 'sortablejs'
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
export default {
  name: 'TrainRe',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      editList: null,
      trainer: null,
      trainee: null,
      deptName: null,
      sortable: null,
      oldList: [],
      newList: [],
      // 参数条件
      listQuery: {
        page: 1,
        limit: 20,
        subject: null
      },
      personlist: null,
      deptlist: null,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create',
        detail: 'Detail'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.fechData()
  },
   methods: {
     fechData() {
      fetchAllCount().then(response => {
        this.total = response.count
      })
      fetchTrainReList(this.listQuery).then(response => {
        this.listLoading = true
        this.list = response.ehstrainreList
        this.showList = response.ehstrainreList
        this.listLoading = false
      })
    },
    handleCreate() {

    },
    handleDownload() {

    },
    showReviewer() {

    },
    handleFilter() {
      this.listQuery.page = 1
      this.fechData()
    },
    handleDelete(row) {
      console.log(row)
      alert(row.id)
      deleteTrainReList(row.id).then(response => {

      })
      this.fechData()
    },
    handleEdit(row) {
      getPersonList().then(response => {
        this.personlist = response.listName
      })
      getDeptList().then(response => {
        this.deptlist = response.listName
      })

      const row1 = JSON.parse(JSON.stringify(row))
      this.temp = Object.assign({}, row1) // copy obj

      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      alert(this.dialogStatus)
      if (this.dialogStatus === 'detail') {
        location.reload()
      } else if (this.dialogStatus === 'update') {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            const query1 = JSON.parse(JSON.stringify(tempData))
            console.log("6666666666666666666666666"+query1.deptName.deptName)
            tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
            const query = { id: query1.id, subject: query1.subject, userName1: query1.trainer.userName, userName2: query1.trainee.userName, deptName1: query1.deptName.deptName }
            editTrainRe(query).then(response => {
               this.obj = response
              this.id = response.id
              this.subject = response.subject
              alert('修改成功')
            })
            // 刷新页面
            location.reload()
          }
        })
      }
    },
    handleDetail(row) {
      const row1 = JSON.parse(JSON.stringify(row))
      this.temp = Object.assign({}, row1) // copy obj

      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'detail'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    setSort() {
      const el = this.list[0]

      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', 
        setData: function(dataTransfer) {
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)

          const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
          this.newList.splice(evt.newIndex, 0, tempIndex)
        }
      })
    }
  }
}
</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>